//
//  SCDebugAccountsVc.m
//  Shuaiche
//
//  Created by 党帅 on 2020/3/4.
//  Copyright © 2020 ShuaiChe. All rights reserved.
//

#import "SCDebugAccountsVc.h"
#import "MBProgressHUD+Add.h"
#import "SCHttpManager.h"

@interface SCDebugAccountsVc ()

@end

@implementation SCDebugAccountsVc

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *key = @"appTestAccount";
    MBProgressHUD *hud = [MBProgressHUD showMessage:nil toView:self.view];
    NSDictionary *param = @{@"key":key};
    [[SCHttpManager shareInstance] httpGet:@"baseprod/config/properties" param:param completion:^(id response, NSError *error){
      if (error) {
          [hud switchShowError:error.localizedDescription];
      } else {
          [hud hide:YES];
          id items = response[key];
          if ([items isKindOfClass:NSArray.class]) {
              self.dataSource = [NSMutableArray arrayWithArray:items];
          } else {
              NSData *data = [items dataUsingEncoding:4];
              if(!data){return;}
              
              NSArray *arr= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
              self.dataSource = [NSMutableArray arrayWithArray:arr];
          }
      }
      [self reloadTableView];
  }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *kCellId = @"CR";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellId];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSDictionary *dic = self.dataSource[indexPath.row];
    cell.textLabel.text = dic[@"title"];
    cell.detailTextLabel.text = dic[@"account"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.row];
    self.selectBlock(dic[@"account"]);
    [self dismissViewControllerAnimated:NO completion:nil];
}

@end
